#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <math.h>
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(
     MARGIN*2+BLOCK_SIZE*BOARD_GRAD_SIZE,
     MARGIN*2+BLOCK_SIZE*BOARD_GRAD_SIZE);//棋盘大小
    initGame();//初始化

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); //设置抗锯齿

    for(int i=0;i<BOARD_GRAD_SIZE+1;i++)
    {
        //从左到右，第(i+1)条竖线
        painter.drawLine(MARGIN+BLOCK_SIZE*i,MARGIN,
                         MARGIN+BLOCK_SIZE*i,this->height()-MARGIN);
        //从上到下，第(i+1)条横线
        painter.drawLine(MARGIN,MARGIN+BLOCK_SIZE*i,
                         this->width()-MARGIN,MARGIN+BLOCK_SIZE*i);
    }
    //绘制选中的棋盘点
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    //防止鼠标出界，后面还有很多类似的判断
    if(clickPosRow>0 && clickPosRow<BOARD_GRAD_SIZE &&
       clickPosCol>0 && clickPosCol<BOARD_GRAD_SIZE &&
        game->gameMapVec[clickPosRow][clickPosCol]==0)
    {

        if(game->playerFlag)//如果为黑棋则黑色不是则是白色
        {
            brush.setColor(Qt::black);
        }
        else
        {
            brush.setColor(Qt::white);
        }
        painter.setBrush(brush);
        painter.drawRect(MARGIN+BLOCK_SIZE*clickPosCol-MARK_SIZE/2,MARGIN+BLOCK_SIZE*clickPosRow-MARK_SIZE/2,MARK_SIZE,MARK_SIZE);
    }
    for(int i=0;i<BOARD_GRAD_SIZE;i++)
    {
        for(int j=0;j<BOARD_GRAD_SIZE;j++)
        {
            if(game->gameMapVec[i][j]==1)
            {
                brush.setColor(Qt::black);
                painter.setBrush(brush);
                painter.drawEllipse(MARGIN+BLOCK_SIZE*j-CHESS_RADIUS,MARGIN+BLOCK_SIZE*i-CHESS_RADIUS,CHESS_RADIUS*2,CHESS_RADIUS*2);
            }
            else if(game->gameMapVec[i][j]==-1)
            {
                brush.setColor(Qt::white);
                painter.setBrush(brush);
                painter.drawEllipse(MARGIN+BLOCK_SIZE*j-CHESS_RADIUS,MARGIN+BLOCK_SIZE*i-CHESS_RADIUS,CHESS_RADIUS*2,CHESS_RADIUS*2);
            }

        }
    }
    //判断输赢
    if(clickPosCol>0 && clickPosCol<BOARD_GRAD_SIZE &&
       clickPosRow>0 && clickPosRow<BOARD_GRAD_SIZE &&
       (game->gameMapVec[clickPosRow][clickPosCol]==1||game->gameMapVec[clickPosRow][clickPosCol]==-1))//落子后判断输赢
    {
        if(game->isWin(clickPosRow,clickPosCol) && game->gameStatus == PLAYING)
        {
            game->gameStatus = WIN;
            QString str;
            str = game->gameMapVec[clickPosRow][clickPosCol]==1?"Black":"White";
            QMessageBox::StandardButton btnValue = QMessageBox::information(this,"winner",str+" win！");
            if(btnValue == QMessageBox::Ok)
            {
                game->startGame(game_type);
                game->gameStatus = PLAYING;
            }
        }
    }
}
//初始化遊戲
void MainWindow::initGame()
{
    game = new GameModel();
    QMessageBox::StandardButton select = QMessageBox::question(this,"激情五子棋","YES：开始对战  NO：退出");
    if(select == QMessageBox::Yes)
    {
        game_type = MAN;
    }
    else
    {
       delete ui ;
    }
    game->gameStatus = PLAYING;
    game->startGame(game_type);
    update();
}



void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
    //跟踪鼠标获取位置
    int x = event->x();
    int y = event->y();

    //棋盘边缘不能落子
    if(x>=MARGIN+BLOCK_SIZE/2&&
           x<this->width()-MARGIN-BLOCK_SIZE/2&&
           y>=MARGIN+BLOCK_SIZE/2&&
           y<this->height()-MARGIN-BLOCK_SIZE/2)
    {
        //获得最近的左上角的点
        int col = (x-MARGIN)/BLOCK_SIZE;
        int row = (y-MARGIN)/BLOCK_SIZE;

        int leftTopPosX = MARGIN+BLOCK_SIZE*col;
        int leftTopPosY = MARGIN+BLOCK_SIZE*row;

        //下面测算点击点到最近的一个棋盘的距离
        clickPosRow = -1; //初始化目标点坐标
        clickPosCol = -1;
        int len = 0;  //最后取整

        selectPos = false;

        //最后选取最近的点
        //与左上角的距离
        len = sqrt((x-leftTopPosX)*(x-leftTopPosX)+(y-leftTopPosY)*(y-leftTopPosY));
        if(len<POS_OFFSET){
            clickPosRow = row;
            clickPosCol = col;
            if(game->gameMapVec[clickPosRow][clickPosCol]==0){
                selectPos = true;
            }
        }
        //右上角
        len = sqrt((x-leftTopPosX-BLOCK_SIZE)*(x-leftTopPosX-BLOCK_SIZE)+(y-leftTopPosY)*(y-leftTopPosY));
        if(len<POS_OFFSET){
            clickPosRow = row;
            clickPosCol = col+1;
            if(game->gameMapVec[clickPosRow][clickPosCol]==0){
                selectPos = true;
            }
        }
        //左下角
        len = sqrt((x-leftTopPosX)*(x-leftTopPosX)+(y-leftTopPosY-BLOCK_SIZE)*(y-leftTopPosY-BLOCK_SIZE));
        if(len<POS_OFFSET){
            clickPosRow = row+1;
            clickPosCol = col;
            if(game->gameMapVec[clickPosRow][clickPosCol]==0){
                selectPos = true;
            }
        }
        //右下角
        len = sqrt((x-leftTopPosX-BLOCK_SIZE)*(x-leftTopPosX-BLOCK_SIZE)+(y-leftTopPosY-BLOCK_SIZE)*(y-leftTopPosY-BLOCK_SIZE));
        if(len<POS_OFFSET){
            clickPosRow = row+1;
            clickPosCol = col+1;
            if(game->gameMapVec[clickPosRow][clickPosCol]==0){
                selectPos = true;
            }
        }
    }
    //存了坐標後也要重繪
    update();
}

void MainWindow::mouseReleaseEvent(QMouseEvent* event)
{
    if(selectPos == false)
    {
        return;
    }
    else
    {
        selectPos = false;
    }
    //棋手下棋
    chessOneByPerson();
    }


void MainWindow::chessOneByPerson()
{
    if(clickPosRow!=-1 && clickPosCol!=-1 && game->gameMapVec[clickPosRow][clickPosCol]==0)
    {
        //在棋盘中落子
        game->actionByPerson(clickPosRow,clickPosCol);

   //实时更新点的位置
        update();
    }
}

